﻿using AiXiu.IDAL;
using AiXiu.Model;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;

namespace AiXiu.DAL
{
    /// <summary>
    /// 账户数据服务类
    /// </summary>
    public class AccountService : IAccountService
    {
        /// <summary>
        /// 用户名是否已注册
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public bool ExistUserName(string userName)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBLogins.Any(m => m.UserName == userName);
        }

        /// <summary>
        /// 手机号是否已注册
        /// </summary>
        /// <param name="mobileNumber">手机号</param>
        /// <returns></returns>
        public bool ExistMobileNumber(string mobileNumber)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBLogins.Any(m => m.MobileNumber == mobileNumber);
        }

        /// <summary>
        /// 通过用户名查找登录信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public TBLogins FindByUserName(string userName)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBLogins.SingleOrDefault(m => m.UserName == userName);
        }

        /// <summary>
        /// 通过手机号查找登录信息
        /// </summary>
        /// <param name="mobileNumber">手机号</param>
        /// <returns></returns>
        public TBLogins FindByMobileNumber(string mobileNumber)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBLogins.SingleOrDefault(m => m.MobileNumber == mobileNumber);
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="login">登录信息</param>
        /// <returns>返回用户Id</returns>
        public int Register(TBLogins login)
        {
            // 准备存储过程参数
            SqlParameter[] sqlParams = {
                new SqlParameter("@userName", login.UserName),
                new SqlParameter("@mobileNumber", login.MobileNumber),
                new SqlParameter("@password", login.Password),
                new SqlParameter("@userId", SqlDbType.Int)
            };
            sqlParams[3].Direction = ParameterDirection.Output;
            // 初始化数据库命令
            AiXiuDbContext dbContext = new AiXiuDbContext();
            DbCommand cmd = dbContext.Database.Connection.CreateCommand();
            cmd.CommandText = "proc_adduser";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(sqlParams);
            // 执行存储过程
            if (cmd.Connection.State != ConnectionState.Open)
            {
                cmd.Connection.Open();
            }
            cmd.ExecuteNonQuery();
            if(cmd.Connection.State == ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
            // 返回结果
            return int.Parse(sqlParams[3].Value.ToString());
        }
    }
}